import math

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cross_decomposition import PLSRegression

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


def as_mfs(x_train_st, y_train):
    n_components = 8
    pls = PLSRegression(n_components=n_components)
    pls.fit(x_train_st, y_train)
    return pls


def err(y_predict, y_test):
    sum = 0
    lens = len(y_predict)
    for i in range(lens):
        e = (y_predict[i] - y_test[i]) ** 2
        # errs.append(e)
        sum += e
    return math.sqrt(sum) / lens


def pre_plt(x_train, y_train, x_test, y_test, y_pre, label, title):
    # Cl/cd plot
    fig_af_mfs_Cl_Cd = plt.figure()
    ax = Axes3D(fig_af_mfs_Cl_Cd)
    ax.scatter(np.array(x_train)[:, 1], np.array(x_train)[:, 0], y_train[:, 1], s=50, c='black', marker='.',
               alpha=0.5,
               label='训练点(' + label + ')')
    ax.scatter(np.array(x_test)[:, 1], np.array(x_test)[:, 0], y_test[:, 1], s=200, c='red', marker='.',
               alpha=0.5,
               label='测试点(' + label + ')')
    ax.scatter(np.array(x_test)[:, 1], np.array(x_test)[:, 0], y_pre[:, 1], s=150, c='blue', marker='+', alpha=0.5,
               label='预测点(' + label + ')')
    ax.set_xlabel("    Re", fontsize=15)
    ax.set_ylabel("   α/(°)", fontsize=15)
    ax.set_zlabel(" Cl/Cd", fontsize=15)
    ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.2f'))
    ax.set_xlim(0, 310000)
    plt.title(title, fontsize=15)
    # 坐标轴数字大小
    plt.tick_params(labelsize=10)
    labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels()

    ax.view_init(azim=-154, elev=28)
    # plt.tight_layout()
    plt.legend(loc=0, fontsize=10, frameon=False)  # 图例位置自
    fig_af_mfs_Cl_Cd.savefig('fig_af_mfs_Cl_Cd.svg', bbox_inches='tight')
    plt.show()

    # Cd
    fig_af_mfs_Cd = plt.figure()
    ax = Axes3D(fig_af_mfs_Cd)
    ax.scatter(np.array(x_train)[:, 1], np.array(x_train)[:, 0], y_train[:, 0], s=50, c='black', marker='.',
               alpha=0.5,
               label='训练点(' + label + ')')
    ax.scatter(np.array(x_test)[:, 1], np.array(x_test)[:, 0], y_test[:, 0], s=200, c='red', marker='.',
               alpha=0.5,
               label='测试点(' + label + ')')
    ax.scatter(np.array(x_test)[:, 1], np.array(x_test)[:, 0], y_pre[:, 0], s=150, c='blue', marker='+', alpha=0.5,
               label='预测点(' + label + ')')
    # ax.set_title(title)
    ax.set_xlabel("    Re", fontsize=15)
    ax.set_ylabel("   α/(°)", fontsize=15)
    ax.set_zlabel(" Cd", fontsize=15)

    plt.tick_params(labelsize=10)
    labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels()
    ax.view_init(azim=-34, elev=28)
    ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.2f'))
    # ax.zaxis.set_major_locator(MultipleLocator(0.03))
    # ax.set_zticklabels(ax.get_zticklabels(),ha='right')
    ax.set_xlim(0, 310000)
    plt.title(title, fontsize=15)
    # ax.set_ylim(-16.00, 15.00)
    # plt.xticks(fontsize=20)
    # plt.yticks(fontsize=20)
    # ax.set_zticks(fontsize=20)

    # plt.tight_layout()
    plt.legend(loc=0, fontsize=10, frameon=False)  # 图例位置自
    fig_af_mfs_Cd.savefig('fig_af_mfs_Cd.svg', bbox_inches='tight')
    plt.show()
